package cn.changeforyou.analyzer.javaparserDemo;

import cn.changeforyou.utils.io.read.ReadUtils;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseException;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ParserConfiguration.LanguageLevel;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.google.common.base.Strings;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.junit.jupiter.api.Test;

/**
 * @author zhyu
 * @date 2021/2/3 15:11
 */
public class ScanProject {

    public static void listClasses(File projectDir) {
        new DirExplorer((level, path, file) -> path.endsWith(".java"), (level, path, file) -> {
            System.out.println(path);
            System.out.println(Strings.repeat("=", path.length()));
            ParserConfiguration parserConfiguration = new ParserConfiguration();
            parserConfiguration.setLanguageLevel(LanguageLevel.JAVA_8);
            JavaParser javaParser = new JavaParser(parserConfiguration);
            CompilationUnit compilationUnit = javaParser.parse(ReadUtils.readReaderToString(new InputStreamReader(new FileInputStream(file), "UTF-8"))).getResult().get();
            new VoidVisitorAdapter<Object>() {
                @Override
                public void visit(ClassOrInterfaceDeclaration n, Object arg) {
                    super.visit(n, arg);
                    System.out.println(" * " + n.getName());
                }
            }.visit(compilationUnit, null);
            System.out.println(); // empty line
        }).explore(projectDir);
    }
    
    public static void main(String[] args) {
        File projectDir = new File("D:\\code\\takeout\\shop");
        listClasses(projectDir);
    }
}
